home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1076 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.5 KB

  1. From: satrajit@iscs.nus.sg (Satrajit Sujit Ghosh)
  2. Message-ID: <4kng7f$kt9@nuscc.nus.sg>
  3. X-Original-Date: 13 Apr 1996 06:05:35 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 14 Apr 96 12:32:30 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Is RTTI required if polymorphic constructor exists
  9. Organization: National University of Singapore
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMXDwaeEDnX0m9pzZAQFnnwF/daFCCOOaQUZxmoMjg40Z/7l0XrhjH8Z9
  13.     OkT3/cQb61GsmtOS0dVO7lPnixGAb9qi
  14.     =tvUc
  15.  
  16. Hi,
  17.     If a polymorphic constructor can be created as demonstrated below, 
  18. is RTTI really necessary, considering that it does not really provide a 
  19. great deal of information.
  20.  
  21. --
  22. Satra[jit]
  23. ------------------------------------------------------
  24. http://www.iscs.nus.sg/~satrajit
  25. Department of Information Systems and Computer Science
  26. National University of Singapore
  27. ======================================================
  28. /*
  29. /////////////////THE POLYMORPHIC CONSTRUCTOR///////////////////
  30. The following code segments demonstrate the creation of a polymorphic 
  31. constructor. 
  32. ///////////////////////////////////////////////////////////////
  33. */
  34.  
  35. #include <iostream.h>
  36. #include <string.h>
  37.  
  38. template<class T>
  39. class Clist{
  40. public:
  41.   Clist(){
  42.     i = 0;
  43.   };
  44.  
  45.   void insert(char *name,T *(*func)()){
  46.     for(int j=0;j<i;j++)
  47.       // prevent re-registration
  48.       if (!strcmp(nm_arr[j],name)){
  49.     return;
  50.       }
  51.  
  52.     nm_arr[i] = strdup(name);
  53.     fun[i] = func;
  54.     i++;
  55.   }
  56.  
  57.   T* get(char *name){
  58.     int j;
  59.     for(j=0;j<i;j++)
  60.       if (!strcmp(nm_arr[j],name)){
  61.     return (*fun[j])();
  62.       }
  63.     return NULL;
  64.   };
  65.  
  66.   // currently can support 5 derived classes
  67.   char *nm_arr[5];
  68.   T *(* fun[5])();
  69.   int i;
  70. };
  71.  
  72.  
  73.  
  74. class CShape{
  75. public:
  76.   virtual void print(){
  77.     cout << "reg" << endl;
  78.   };
  79.  
  80.   static CShape * mk_subclass(char *str){
  81.     return reg.get(str);
  82.   }
  83.  
  84. protected:
  85.   static int reg_sub(char *name,CShape *(*func)()){
  86.     reg.insert(name,func);
  87.     return 0;
  88.   };
  89.  
  90. private:  
  91.   static Clist<CShape> reg;
  92.  
  93. };
  94.  
  95. Clist<CShape> CShape::reg;
  96.  
  97. class CCircle:public virtual CShape{
  98. public:
  99.   virtual void print(){
  100.     cout <<"CCircle";
  101.   };
  102.  
  103.   static CShape* int_new(){
  104.     CShape *p = new CCircle;
  105.     return p;
  106.   };
  107. private:
  108.   static int i;
  109. };
  110.  
  111. int CCircle::i = reg_sub("CCircle",&CCircle::int_new);
  112.  
  113. class CRect:public virtual CShape{
  114. public:
  115.  
  116.   virtual void print(){
  117.     cout <<"CRect";
  118.   };
  119.  
  120.   static CShape* int_new(){
  121.     CShape *p = new CRect;
  122.     return p;
  123.   };
  124.  
  125. private:
  126.   static int i;
  127. };
  128.  
  129. int CRect::i =  reg_sub("CRect",&CRect::int_new);
  130.  
  131. class CSquare:public virtual CRect{
  132. public:
  133.  
  134.   virtual void print(){
  135.     cout <<"CSquare";
  136.   };
  137.  
  138.   static CShape* int_new(){
  139.     CShape *p = new CSquare;
  140.     return p;
  141.   };
  142.  
  143. private:
  144.   static int i;
  145. };
  146.  
  147. int CSquare::i =  reg_sub("CSquare",&CSquare::int_new);
  148.  
  149.  
  150. int 
  151. main(){
  152.   CShape *fg = CShape::mk_subclass("CCircle");
  153.   cout << "Testing constructed CCircle [";
  154.   fg->print();
  155.   cout << "]\n";
  156.   CShape *fg1 = CShape::mk_subclass("CRect");
  157.   cout << "Testing constructed CRect [";
  158.   fg1->print();
  159.   cout << "]\n";
  160.   CShape *fg2 = CShape::mk_subclass("CSquare");
  161.   cout << "Testing constructed CSquare [";
  162.   fg2->print();
  163.   cout << "]\n";
  164.   return 0;
  165. }
  166. ---
  167. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  168. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  169. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  170. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  171. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  172.